import { _decorator, Component, Node, SpringJoint2D, Sprite, UITransform,Animation, AnimationClip, animation, SpriteFrame, random } from 'cc';
import { IEntity, ISpikes } from '../../levels';
import { PlayerStateMachine } from '../../Scripts/Player/PlayerStateMachine';
import { DIRECTION_ENUM, DIRECTION_ORDER_ENUM, ENTITY_TYPE_ENUM, ENTITY_STATE_ENUM, PARAMS_NAME_ENUM, SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM, EVENT_ENUM } from '../../Enums';
import { TILE_HEIGHT, TILE_WIDTH } from '../../Scripts/Tile/TileManager';
import { randomByLen } from '../../Utils';
import { SpikesStateMachine } from './SpikesStateMachine';
import EventManager from '../../Runtime/EventManger';
import DataManager from '../../Runtime/DataManager';
const { ccclass, property } = _decorator;


@ccclass('SpikesManager')
export class SpikesManager extends Component {
    id:string = randomByLen(12)
    x: number = 0                           // 当前X坐标
    y: number = 0                           // 当前Y坐标
    fsm: PlayerStateMachine                 // 状态机实例
    private _count: number      // 方向
    private _totalCount:number        // 状态
    type:ENTITY_TYPE_ENUM           // 实体类型

    get count(){
      return this._count
    }

    set count(newCount:number){
      this._count=newCount
      this.fsm.setParams(PARAMS_NAME_ENUM.SPIKES_CUR_COUNT,newCount)
    }
    get totalCount(){
      return this._totalCount
    }

    set totalCount(newCount:number){
      this._totalCount=newCount
      this.fsm.setParams(PARAMS_NAME_ENUM.SPIKES_TOTAL_COUNT,newCount)
    }

    // 初始化
    async init(params:ISpikes) {
      // 1. 设置精灵组件
      const sprite = this.addComponent(Sprite)
      sprite.sizeMode = Sprite.SizeMode.CUSTOM

      // 2. 设置尺寸
      const transform = this.getComponent(UITransform)
      transform.setContentSize(TILE_WIDTH * 4, TILE_HEIGHT * 4)

      // 3. 初始化状态机
      this.fsm = this.addComponent(SpikesStateMachine)
      await this.fsm.init()


      this.x=params.x
      this.y=params.y
      this.type=params.type
      this.totalCount=SPIKES_TYPE_MAP_TOTAL_COUNT_ENUM[this.type]
      this.count=params.count

      EventManager.Instance.on(EVENT_ENUM.PLAYER_MOVE_END,this.onLoop,this)
    }

    onDestroy(){
      super.destroy()
      EventManager.Instance.off(EVENT_ENUM.PLAYER_MOVE_END,this.onLoop)
    }

    update() {
      // 设置节点位置（包含偏移计算）
      this.node.setPosition(
        this.x * TILE_WIDTH - 1.5 * TILE_WIDTH,
        -this.y * TILE_HEIGHT + 1.5 * TILE_HEIGHT
      )
    }

    onLoop(){
      if(this.count===this.totalCount){
        this.count=1
      }else{
        this.count++
      }
      this.onAttack()
    }

    backZero(){
      this.count=0
    }

    onAttack(){
      if(!DataManager.Instance.player){
        return
      }

      const {x:playerX,y:playerY}=DataManager.Instance.player
      if(this.x===playerX&&this.y===playerY&&this.count===this.totalCount){
        EventManager.Instance.emit(EVENT_ENUM.ATTACK_PLAYER,ENTITY_STATE_ENUM.DEATH)
      }
    }
}
